<?php

namespace App\Store\Models\Goods;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;

class GoodsGlodprice extends Model
{
    protected $table = 'site_goods_glodprice';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo','categoryinfo'])->where(['cp_id' => $cp_id, 'is_del' => 1])->orderBy('date_str', 'desc')->orderBy('create_at', 'desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }
        $ids=$request->ids;
        $ids=is_array($ids)?$ids:(is_array(json_decode($ids,true))?json_decode($ids,true):[]);
        if(count($ids)>0){
            $query->whereIn('id',$ids);
        }

        $store_id = $request->store_id;//下拉单选搜索
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }

        $goods_category_id = $request->goods_category_id;//下拉单选搜索
        if ($goods_category_id>0) {
            $query->where('goods_category_id', $goods_category_id);
        }


        $goods_category_name = $request->goods_category_name;
        if ($goods_category_name != '') {
            $goods_category_ids=GoodsCategory::where(['cp_id' => $cp_id, 'is_del' => 1])->where('category_name', 'like', '%' . $goods_category_name . '%')->pluck('id');
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        $date_start = $request->date_start;
        $date_end = $request->date_end;
        if ($date_start && $date_end) {
            $query->where('date_str', '>=',$date_start)->where('date_str', '<=',$date_end);
        }

        $start_time = $request->start_time;
        $end_time = $request->end_time;
        if ($start_time && $end_time) {
            $query->where('create_at', '>=',strtotime($start_time))->where('create_at', '<=',strtotime($end_time));
        }

        $isgetall = intval($request->isgetall) ?? 0;


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id','store_name');
    }

    public function categoryinfo(){
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id','category_name');
    }
}
